using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Windows.Forms;

namespace SPDevExplorer
{
    internal class ServiceManager
    {
        private Dictionary<string, SPDevExplorerWebService.DevServices> _devServices = null;

        public SPDevExplorerWebService.DevServices GetService(TreeNode node)
        {
            if (_devServices == null) _devServices = new Dictionary<string, SPDevExplorerWebService.DevServices>();


            while (node.Parent != null)
                node = node.Parent;

// SvdO: node.Text now contains full url, don't prefix with http://
#if false
            return _devServices["http://" + node.Text];
#else
            Trace.WriteLine(String.Format("ServiceManager.GetService: url = {0}", node.Text));
            return _devServices[node.Text];
#endif
// /SvdO
        }

        public bool Exists(string Url)
        {
            if (_devServices == null) return false;

            if (_devServices.ContainsKey(Url.ToLower())) return true;

            return false;
        }

        public void Remove(string Url)
        {
            if (_devServices == null) return;

            if (_devServices.ContainsKey(Url.ToLower()))
                _devServices.Remove(Url.ToLower());
        }

        public SPDevExplorerWebService.DevServices Create(string UrlOfService, string domain, string userName,
                                                          string password)
        {
            string Url = UrlOfService.ToLower();

            if (_devServices == null) _devServices = new Dictionary<string, SPDevExplorerWebService.DevServices>();

            if (_devServices.ContainsKey(Url)) return _devServices[Url];

            SPDevExplorerWebService.DevServices oServices = new SPDevExplorerWebService.DevServices();
            //oServices.Url = Url + "/_layouts/devservices.asmx";

            if (!string.IsNullOrEmpty(userName))
                oServices.Credentials = new NetworkCredential(userName, password, domain);
            else
                oServices.UseDefaultCredentials = true;

            // SvdO: Fails because CoockieContainer is null, first test
            if (oServices.CookieContainer != null)
            {
                oServices.CookieContainer.GetCookies(new Uri(UrlOfService));
            }
            // /SvdO


            // SvdO: Test if the given url is a food Uri
            try
            {
                Uri siteUri = new Uri(Url);
            }
            catch (Exception ex)
            {
                throw new Exception(String.Format("Uri '{0}' is not a valid SharePoint site url. Exception: {1}", Url,
                                                  ex.ToString()));
            }
            // /SvdO

            // SvdO: make sure that url's for constructing the webservice url are combined "the correct way"
            string webServiceUrl = Url;
            if (!Url.EndsWith("/"))
            {
                webServiceUrl = Url + "/";
            }
            webServiceUrl += "_vti_bin/devservices.asmx";

            oServices.Url = webServiceUrl;

            Trace.WriteLine(String.Format("ServiceManager.Create: adding url = {0}, with webservice url = {1}", Url,
                                          oServices.Url));
            _devServices.Add(Url, oServices);

            return oServices;
        }
    }
}